﻿

#region USING
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//
#endregion USING


namespace Gsp315_AIBehaviors
{
    public class SIM_Controller_OLD
    {
        #region DATA_MEMBERS
        //
        private int maxRock, maxPaper, maxScissor;

        private static List<Agent> aliveAgents;
        private static List<Agent> deadAgents;
        //
        #endregion DATA_MEMBERS



        #region OBJECT_CONSTRUCTION
        //
        //-------------------------------------------------------------------------
        //Default Contructor
        public SIM_Controller_OLD(Agent rock, Agent paper, Agent scissor)
        {
            //Max # of agents...
            maxRock = maxPaper = maxScissor = 1;

            aliveAgents = new List<Agent>();   //instantiate Alive Agent List
            deadAgents = new List<Agent>();    //instantiate Dead Agent List

            init(rock, paper, scissor);
        }

        //-------------------------------------------------------------------------
        //Initial agents to load on screen by default, when program launches
        private void init(Agent r, Agent p, Agent s)
        {
            Agent rock, paper, scissor;
            
            Random xPosSeed = new Random((int)DateTime.Now.Ticks);
            Random yPosSeed = new Random((int)DateTime.Now.Millisecond);

            Random thetaSeed = new Random((int)DateTime.Now.Second);
            
            for (int i = 0; i < maxRock; ++i)
            {
                rock = new Agent(r.Texture); //, r.Position, r.Size);
                paper = new Agent(p.Texture); //, p.Position, p.Size);
                scissor = new Agent(s.Texture); //, s.Position, s.Size);

                //Set Agent Index...
                rock.Index = paper.Index = i;//scissor.Index = i;

                //Update Agent Type...
                rock.AgentType = agent_Type.ROCK;
                paper.AgentType = agent_Type.PAPER;
                scissor.AgentType = agent_Type.SCISSOR;

                //Random Position...
                rock.Position = Helper.Position_Random(10, 760, 10, 560);  //(xPosSeed, yPosSeed, 40, 760, 40, 560);
                paper.Position = Helper.Position_Random(10, 760, 10, 560);  //(xPosSeed, yPosSeed, 40, 760, 40, 560); 
                scissor.Position = Helper.Position_Random(10, 760, 10, 560);  //(xPosSeed, yPosSeed, 40, 760, 40, 560);

                //Random Angle of Movement...
                //obj direction in vector is internally updated, when angleInRadion is updated
                rock.AngleInRadians = Helper.AngleInRadians_Random(thetaSeed);
                paper.AngleInRadians = Helper.AngleInRadians_Random(thetaSeed);
                scissor.AngleInRadians = Helper.AngleInRadians_Random(thetaSeed);
                
                aliveAgents.Add(rock);
                aliveAgents.Add(paper);
                aliveAgents.Add(scissor);
            }
        }
        //
        #endregion OBJECT_CONSTRUCTION



        #region PROPERTIES
        //
        public int RockScore { get { return maxRock; } }
        //
        public int PaperScore { get { return maxPaper; } }
        //
        public int ScissorScore { get { return maxScissor; } }
        //
        public List<Agent> DeadAgents { get { return deadAgents; } }
        //
        public List<Agent> AliveAgents { get { return aliveAgents; } }
        //
        #endregion PROPERTIES


        
        #region PRIVATE_METHODS
        //
        //-------------------------------------------------------------------------
        //Called in SpawnAgent Method
        private void AddAgent(agent_Type type)
        {
            Random xPosSeed = new Random((int)DateTime.Now.Ticks);
            Random yPosSeed = new Random((int)DateTime.Now.Millisecond);

            Random thetaSeed = new Random((int)DateTime.Now.Second);

           //foreach (Player agent in deadAgents)
            for (int i = 0; i < deadAgents.Count; ++i)
            {
                if (deadAgents[i].AgentType == type) 
                {
                    deadAgents[i].IsAlive = true;
                    deadAgents[i].Position = Helper.Position_Random(xPosSeed, yPosSeed, 50, 750, 50, 550);
                    deadAgents[i].AngleInRadians = Helper.AngleInRadians_Random(thetaSeed); 
                    
                    //Update the Alive/Dead agent Lists
                    aliveAgents.Add(deadAgents[i]); 
                    deadAgents.Remove(deadAgents[i]); 
                    break;
                }
            }
        }

        //-------------------------------------------------------------------------
        //Spawn an agent ONLY if there is an extinct species
        //Called in Update Method, after 'UpdateAliveAgentCount()' method
        private void SpawnAgent()
        {
            if (maxRock <= 0)
            {
                AddAgent(agent_Type.ROCK);
                maxRock = 1;
            }
            //
            if (maxPaper <= 0)
            {
                AddAgent(agent_Type.PAPER);
                maxPaper = 1;
            }
            //
            if (maxScissor <= 0)
            {
                AddAgent(agent_Type.SCISSOR);
                maxScissor = 1;
            }
        }

        //-------------------------------------------------------------------------
        //Update the agent count still Alive
        //Called in Update Method
        private void UpdateAliveAgentCount(Agent agent)
        {
            switch ((int)agent.AgentType)
            {
                case (int)agent_Type.ROCK:
                    --maxRock;
                    break;
                case (int)agent_Type.PAPER:
                    --maxPaper;
                    break;
                case (int)agent_Type.SCISSOR:
                    --maxScissor;
                    break;
            }
        }
        //
        #endregion PRIVATE_METHODS



        #region PUBLIC_METHODS
        //
        //-------------------------------------------------------------------------
        //Read-Only access 'AliveAgents' List
        public static List<Agent> EntityList_Alive
        {
            get { return aliveAgents; }
        }

        //-------------------------------------------------------------------------
        //Update DeadAgents, AliveAgents List, Program States, and Agent(s) Stats
        public void Update(GameTime gameTime)
        {
            //foreach (Player agent in aliveAgents)
            for (int i = 0; i < aliveAgents.Count; ++i)
            {
                aliveAgents[i].Update_Behaviour(ref aliveAgents, gameTime);

                if (!aliveAgents[i].IsAlive)//IF agent=>DEAD
                {
                    //deadAgents.Add(aliveAgents[i]);         //Add 'dead agent' in DeadAgents List

                    //UpdateAliveAgentCount(aliveAgents[i]);  //Update Alive Agents Count
                    aliveAgents.Remove(aliveAgents[i]);     //Remove dead agent fom Alive agent List
                    SpawnAgent();                           //Spawn an agent ONLY if there is an extinct species
                }
            }
        }

        //-------------------------------------------------------------------------
        //Render AliveAgents, Agent(s) Stats to screen
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Agent agent in aliveAgents)
            {
                agent.Draw(spriteBatch);
                //spriteBatch.Draw(agent.Texture, agent.Position, Color.White, agent.Direction,);
            }
        }
        //
        #endregion PUBLIC_METHODS

    }//class
}//namespace
